package com.hbzd.protection.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hbzd.protection.entity.Employee;
import com.hbzd.protection.mapper.EmployeeMapper;
import com.hbzd.protection.util.InitData;

@Service
@Transactional
public class EmployeeServiceImpl {
	
	@Resource
	private EmployeeMapper employeeMapper;
	
	/**
	 * 1.通过手机号判断该用户是否存在，若不存在，添加；若存在，判断密码是否正确
	 * @param employee
	 * @return
	 */
	public String handleData(Employee employee) {
		if(employee.getPhone() == null || employee.getPassword() == null){
			return "valid password!";
		}
		Employee source = employeeMapper.getByPhone(employee.getPhone());
		if(source != null){
			//判断用户密码是否正确
			if(source.getPassword() == null && InitData.PASSWORD.equals(employee.getPassword()) || source.getPassword().equals(employee.getPassword())){
				employeeMapper.update(employee);
				return "success";
			}else{
				return "valid password!";
			}
		}else{
			//用户初始化密码
			if(InitData.PASSWORD.equals(employee.getPassword())){
				//添加用户
				employeeMapper.add(employee);
				return "success";
			}else{
				return "no privilage";
			}
		}
	}
	/**
	 * 判断该openid是否已经绑定
	 * @param openid
	 * @return
	 */
	public boolean authenticate(String openid) {
		Employee employee = employeeMapper.getByOpenId(openid);
		return employee != null ? true : false;
	}
	public String modifyPassword(Employee employee,String password1) {
		Employee source = employeeMapper.getByOpenId(employee.getOpenid());
		if(source != null && source.getPassword().equals(employee.getPassword())){
			employee.setPassword(password1);
			employeeMapper.modifyPassword(employee);
			return "success";
		}else{
			return "invalid";
		}
		
		
	}
	public String update(Employee employee) {
		Employee source = employeeMapper.getByOpenId(employee.getOpenid());
		if(source != null && source.getPassword().equals(employee.getPassword())){
			employeeMapper.updateOpenid(employee);
			return "success";
		}else{
			return "invalid";
		}
	}

}
